import { useCallback } from 'react'
import { h0 } from './fp'
import { nextDate, prevDate } from '../query/store/actions'

export default function useNav(departDate, dispatch) {
  const isPrevDisabled = h0(departDate) <= h0()
  const isNextDisabled = h0(departDate) - h0() > 20 * 86400 * 1000

  const prev = useCallback(() => {
    if (isPrevDisabled) {
      return
    }
    dispatch(prevDate())
  }, [dispatch, isPrevDisabled])

  const next = useCallback(() => {
    if (isNextDisabled) {
      return
    }
    dispatch(nextDate())
  }, [dispatch, isNextDisabled])

  return {
    isPrevDisabled,
    isNextDisabled,
    prev,
    next,
  }
}
